import { QXEvent } from 'qx-util';
import { WinMessageItem } from '../../entities';
import { MessageType } from '../../enums';

/**
 * window.postMessage封装
 *
 * @author chitanda
 * @date 2021-06-06 16:06:13
 * @export
 * @class WinMessageBase
 */
export class WinMessageBase {
    /**
     * 消息事件
     *
     * @author chitanda
     * @date 2021-06-06 16:06:04
     * @type {QXEvent<{ message: WinMessageItem }>}
     */
    readonly evt: QXEvent<{ [MessageType.打开视图]: WinMessageItem,[MessageType.画布新建]: WinMessageItem, [MessageType.打开应用视图]: WinMessageItem }> = new QXEvent();

    /**
     * Creates an instance of WinMessageBase.
     *
     * @author chitanda
     * @date 2021-06-07 09:06:46
     */
    constructor() {
        this.listener();
    }

    /**
     * 事件可用状态
     *
     * @type {boolean}
     * @memberof WinMessageBase
     */
     eventEnable:boolean = true;

    /**
     * 监听消息事件
     *
     * @author chitanda
     * @date 2021-06-06 16:06:27
     * @protected
     */
    protected listener(): void {
        addEventListener('message', (e: MessageEvent) => {
            if (e.data) {
                const data = e.data as WinMessageItem;
                if (data.type === MessageType.打开视图) {
                    this.evt.emit(MessageType.打开视图, e.data);
                }
                if (data.type === MessageType.打开应用视图) {
                    this.evt.emit(MessageType.打开应用视图, e.data);
                }
                if (data.type === MessageType.画布新建) {
                    this.evt.emit(MessageType.画布新建, e.data);
                }
            }
        });
    }

    /**
     * 发送消息
     *
     * @author chitanda
     * @date 2021-06-06 16:06:12
     * @protected
     * @param {WinMessageItem} item
     */
    protected sendMessage(item: WinMessageItem): void {
        try {
            if (window.parent) {
                window.parent.postMessage(item, location.origin);
            }
        } catch (err) {
            console.error(err);
        }
    }

    /**
     * 通知父打开某视图
     *
     * @author chitanda
     * @date 2021-06-06 16:06:48
     * @param {string} dataType 数据类型
     * @param {*} [context] 上下文
     * @param {*} [params] 参数
     * @param {any[]} [items] 当前数据
     * @return {*}  {void}
     */
    openView(dataType: string, context?: any, params?: any, items?: any[]): void {
        const item = new WinMessageItem({
            type: MessageType.打开视图,
            dataType,
            context,
            params,
            data: items,
        });
        this.sendMessage(item);
    }

    /**
     * 通知父打开指定应用视图
     *
     * @author chitanda
     * @date 2024-01-29 16:01:16
     * @param {string} dataType
     * @param {*} [context]
     * @param {*} [params]
     * @param {any[]} [items]
     */
    openAppView(dataType: string, context?: any, params?: any, items?: any[]): void {
      const item = new WinMessageItem({
          type: MessageType.打开应用视图,
          dataType,
          context,
          params,
          data: items,
      });
      this.sendMessage(item);
  }


     /**
      * 通知父打开画布新建视图
      *
      * @param {string} dataType
      * @param {*} [context]
      * @param {*} [params]
      * @param {any[]} [items]
      * @memberof WinMessageBase
      */
     openCanvasCreateView(dataType: string, context?: any, params?: any, items?: any[]): void {
        const item = new WinMessageItem({
            type: MessageType.画布新建,
            dataType,
            context,
            params,
            data: items,
        });
        this.sendMessage(item);
    }
}
